package ModelLayer;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * Write a description of class AddressBookContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AddressBookContainer
{
    // instance variables - replace the example below with your own
    private ArrayList<Person> personer;
    private static AddressBookContainer instance; //Singleton

    /**
     * Singleton constructor
     */
    private AddressBookContainer()
    {      
        personer = new ArrayList<Person>();
    }

    //     public AddressBookContainer()
    //     {
    //         personer = new ArrayList<Person>();
    //     }

    //Singleton
    public static AddressBookContainer getInstance()
    {
        if(instance == null)
        {
            instance = new AddressBookContainer();
        }
        return instance;
    }

    //add person to ArrayList<Person>
    public void addPerson(Person c)
    {
        personer.add(c);
    }

    //find person in ArrayList<Person>
    public Person getPerson(long r)
    {
        
            Person person = null;
            int index = 0;
            int rememberIndex = 0;
            boolean found = false;

            String id = Long.toString(r);
            if(personer != null)
        {
            while(index < personer.size() && !found)
            {
                person = personer.get(index);

                if(Long.toString(person.getID()).equals(id))
                {
                    found = true;
                    rememberIndex = index;
                }
                index++;
            }
            if(found)
            {
                return personer.get(rememberIndex);
            }
            else
            {
                return null;
            }
        }
        else 
        {
            return null;
        }
    }

    //update person in ArrayList<Person>
    public void UpdatePerson(long u, long id, String name, String address, int postalCode, String city, int phone)
    {
        Person person = null;
        int index = 0;
        int rememberIndex = 0;
        boolean found = false;

        String id1 = Long.toString(u);

        while(index < personer.size() && !found)
        {
            person = personer.get(index);

            if(Long.toString(person.getID()).equals(id1))
            {
                found = true;
                rememberIndex = index;
            }
            index++;
        }
        if(found)
        {
            person = personer.get(rememberIndex);
            person.setID(id);
            person.setName(name);
            person.setAddress(address);
            person.setPostalCode(postalCode);
            person.setCity(city);
            person.setPhone(phone);
            System.out.println("the person with ID :"+id+" are updated");
        }
        else
        {
            System.out.println("The ID"+id1+" of person dosen't exist");
        }

    }

    public void deletePerson(long id)
    {
        int index = 0;
        int rememberIndex = 0;
        boolean found = false;
        Person person = null;
        String id1 = Long.toString(id);

        while(index < personer.size() && !found)
        {
            person = personer.get(index);

            if(Long.toString(person.getID()).equals(id1))
            {
                found = true;
                rememberIndex = index;
            }
            index++;

        }
        if(found)
        {
            personer.remove(rememberIndex);
            System.out.println(id1);
        }
        else
        {
            System.out.println("The ID: "+id1+" of person dosen't exist");
        }

    }

    public void listAllPerson()
    {
        for(Person filename : personer)
        {
            filename.printDetail();

        }
    }
    
    public int getSize()
    {
        return personer.size();
    }


}
